package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpModel;
import com.monitor.service.ErpModelService;
import com.monitor.service.ErpSpecificationsService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品牌型号表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-13
 */
@RestController
@RequestMapping("/erpModel")
@Api(description="品牌型号")
public class ErpModelController {

    @Resource
    private ErpModelService modelService;

    @Resource
    private ErpSpecificationsService specificationsService;
   /* @RequestMapping("/getModelToTree")
    @ApiOperation("查询品牌型号信息得到树状结构")
    @ApiImplicitParams({
           *//* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*//*
    })
    public @ResponseBody
    Result<List<ErpModel>> getModelToTree() {
        Result<List<ErpModel>> result=new Result<List<ErpModel>>();
        try{
            List<ErpModel> modelList=new ArrayList<ErpModel>();
            List<ErpModel> models=modelService.selectList(new EntityWrapper<ErpModel>().eq("iszh",0));
            for (ErpModel model:models) {
                modelList.add(modelService.getChildModel(model));
            }
            result.setCount(models.size());
            result.setData(models);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

*/

    @RequestMapping("/inertOrUpdateModelCustomer")
    @ApiOperation("添加Or修改品牌型号信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateModelCustomer(ErpModel Model) {
        Result<Integer> result=new Result<Integer>();

        try{
            if (Model.getAddOrXiuGai()==false){
                ErpModel erpModel = modelService.selectById(Model.getId());
                Model.setZid(erpModel.getZid());
                result.setData( modelService.updateById(Model)?1:0);
            }else {
                result.setData(modelService.insert(Model)?1:0);
            }
            //SF01542100591005
            result.setCount(1);

        }catch (Exception e){e.printStackTrace();}
        return result;
    }    @RequestMapping("/updateModelCustomer")
    @ApiOperation("修改品牌型号信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> updateModelCustomer(ErpModel Model) {
        Result<Integer> result=new Result<Integer>();

        try{


            result.setCount(1);
            result.setData(modelService.updateById(Model)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getModelCustomerById")
    @ApiOperation("根据品牌型号id品牌型号信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "ModelId", value = "品牌型号ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpModel> getModelCustomerById(Integer ModelId) {
        Result<ErpModel> result=new Result<ErpModel>();
        try{
            result.setCount(1);
            result.setData(modelService.selectById(ModelId));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeModelCustomerById")
    @ApiOperation("根据品牌型号id删除品牌型号信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "ModelId", value = "品牌型号ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeModelCustomerById(Integer ModelId) {
        Result<Integer> result=new Result<Integer>();
        try{
            ErpModel erpModel = modelService.selectById(ModelId);
            result.setData(modelService.deleteById(ModelId)?1:0);
            if (true){
                List<ErpModel> pid = modelService.selectList(new EntityWrapper<ErpModel>().eq("pid", erpModel.getZid()));
                List<String>   ids=new ArrayList<>();
                if (pid.size()!=0){
                    pid.parallelStream().forEach(p->{
                        ids.add(p.getId()+"");
                    });
                }
                if (ids.size()!=0){
                modelService.deleteBatchIds(ids);
                }
                if (pid.size()!=0)
                pid.parallelStream().forEach(spec->{
                    if (spec!=null){
                        List<ErpModel> pid1 = modelService.selectList(new EntityWrapper<ErpModel>().eq("pid", spec.getZid()));
                        List<String>   idss=new ArrayList<>();
                        if (pid1.size()!=0)
                            pid1.parallelStream().forEach(p->{
                                idss.add(p.getId()+"");
                            });
                        if (idss.size()!=0)
                            modelService.deleteBatchIds(idss);
                        if (pid1.size()!=0)
                        pid1.parallelStream().forEach(specc->{
                            List<ErpModel> pid2 = modelService.selectList(new EntityWrapper<ErpModel>().eq("pid", specc.getZid()));
                            List<String>   idsss=new ArrayList<>();
                            if (pid2.size()!=0)
                                pid2.parallelStream().forEach(p->{
                                    idsss.add(p.getId()+"");
                                });
                            if (idsss.size()!=0)
                            modelService.deleteBatchIds(idsss);
                            if (pid2.size()!=0)
                            pid2.parallelStream().forEach(speccc->{
                                List<ErpModel> pid3 = modelService.selectList(new EntityWrapper<ErpModel>().eq("pid", speccc.getZid()));
                                List<String>   idssss=new ArrayList<>();
                                if (pid3.size()!=0)
                                    pid3.parallelStream().forEach(p->{
                                        idssss.add(p.getId()+"");
                                    });
                                if (idssss.size()!=0)
                                modelService.deleteBatchIds(idssss);
                            });
                        });
                    }
                });
            }





            result.setCount(1);

        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getModelCustomerByModelIdPage")
    @ApiOperation("根据品牌型号ID查询品牌型号分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "ModelId", value = "合同ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Page<ErpModel>> getModelCustomerBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                                      @RequestParam(value="limit",defaultValue="10") int limit) {
        Result<Page<ErpModel>> result=new Result<Page<ErpModel>>();
        try{
            Page<ErpModel> page1=new Page<ErpModel>(page,limit);
            Page<ErpModel> carPage=modelService.selectPage(page1,new EntityWrapper<ErpModel>());
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getModelCustomerByModelIdList")
    @ApiOperation("查询所有品牌型号")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "ModelId", value = "合同ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<List<ErpModel>> getModelCustomerBycustIdList() {
        Result<List<ErpModel>> result=new Result<List<ErpModel>>();
        try{
            List<ErpModel> carPage=modelService.getModelCustomerBycustIdList();
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


}

